import json

'''
不完善，绝对有bug
'''
def merge_dict(template: dict, datas: dict):
    for data_key in list(datas.keys()):
        if data_key not in template:
            datas.pop(data_key)
        elif not datas[data_key]:
            if data_key != 'verify':
                datas.pop(data_key)
            else:
                if datas[data_key] is not None and isinstance(datas[data_key], template[data_key]):
                    pass
                else:
                    datas.pop(data_key)
        else:
            if isinstance(datas[data_key], dict) and isinstance(template[data_key], dict):
                merge_dict(template[data_key], datas[data_key])
            elif isinstance(datas[data_key], list) and isinstance(template[data_key], list):
                for data_list in datas[data_key]:
                    merge_dict(template[data_key][0], data_list)
            elif not isinstance(datas[data_key],template[data_key]):
                datas.pop(data_key)
    return datas


if __name__ == '__main__':
    template = {
        'config': {
            'name': str,
            'base_url': str,
            'variables': dict,
            'parameters': dict,
            'verify': bool,
            'export': list
        },
        'teststeps': [{
            'name': str,
            'variables': list,
            'extract': dict,
            'validate': list,
            'setup_hooks': list,
            'teardown_hooks': list,
            'request': {
                'method': str,
                'url': str,
                'params': list,
                'headers': dict,
                'cookies': dict,
                'data': dict,
                'json': dict
            },
        }]
    }
    data = {
        "config": {
            "project": {
                "id": 1,
                "admin": {
                    "date_joined": "2021-11-02T12:57:17.610002Z",
                    "email": "haiwen@sqtest.com",
                    "id": 1,
                    "is_active": True,
                    "is_superuser": True,
                    "phone": "13512354875",
                    "realname": "海文",
                    "username": "haiwen",
                    "user_type": 1
                },
                "name": "测开3期",
                "status": 0,
                "version": "v3",
                "desc": "测开3期",
                "create_time": "2021-11-02 14:20:24",
                "update_time": "2021-11-02 14:20:24"
            },
            "name": "case004",
            "base_url": "localhost",
            "variables": {},
            "parameters": {},
            "export": ['nihao','nibuhao'],
            "verify": False
        },
        "teststeps": [
            {
                "name": ["step_name"],
                "variables": {},
                "request": {
                    "method": "GET",
                    "url": "/demo/path",
                    "params": None,
                    "headers": None,
                    "json": None,
                    "data": None
                },
                "extract": {},
                "validate": [],
                "setup_hooks": [],
                "teardown_hooks": [],
                "belong_case_id": 1,
                "sorted_no": 1
            }
        ],
        "desc": "testcase666",
        "id": 1,
        "file_path": "测开3期_case001.json",
        "create_time": "2021-11-04T13:01:04.720807Z",
        "update_time": "2021-11-09T12:49:20.941195Z"
    }
    res = merge_dict(template, data)
    print(json.dumps(res,indent=4))
